<template>
  <div>
    <a-form layout="inline" class="default-search-form gb-flex-form" :form="form" @submit="handleSearch">
      <a-row :gutter="8">
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" :label="$t('param_code')">
            <a-input v-decorator="['paramCode']" :placeholder="$t('param_code')"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" :label="$t('param_name')">
            <a-input v-decorator="['paramName']" :placeholder="$t('param_name')"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item class="gb-search-button">
            <a-button class="item" v-if="funcs['ParamSearch']" type="primary" html-type="submit">
              {{ $t(funcs['ParamSearch']) }}
            </a-button>
            <a-button type="primary" class="item" @click="handleReset">
              {{ $t('reset') }}
            </a-button>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <a-table :columns="columns"
      :rowKey="record => record.id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      size="small"
      :scroll="{ x: 1200 }"
    >
      <div slot="long_column_ellipsis" slot-scope="text">
        <LongTableColumn width="200">{{ text }}</LongTableColumn>
      </div>

      <div class="table-single-operations" slot="action" slot-scope="text, record">
        <ParamForm v-if="funcs['ParamUpdate']" class="item" :text="$t(funcs['ParamUpdate'])" :row="record" :callback="handleSearch" />
      </div>
    </a-table>
  </div>
</template>
<script>
import axios from 'axios'
import common from '@/global/common.js'

export default {
  data() {
    return {
      form: this.$form.createForm(this, {name: this._uid}),
      data: [],
      loading: true,
      pagination: {sortField: 'param_code', sortOrder: 'ascend', ...common.getPagination()},
    }
  },
  computed: {
    funcs() {
      return common.getVisibleFunc('ParamList')
    },
    columns() {
      const columns = [
        { title: this.$t("param_code"), dataIndex: 'paramCode', sorter: true, defaultSortOrder:'ascend', key: 'param_code', width: 200, },
        { title: this.$t("param_name"), dataIndex: 'paramName', sorter: true, key: 'param_name', scopedSlots: { customRender: 'long_column_ellipsis' }, width: 200 },
        { title: this.$t("param_value"), dataIndex: 'paramValue', sorter: true, key: 'param_value', scopedSlots: { customRender: 'long_column_ellipsis' }, width: 200 },
        { title: this.$t("create_by"), dataIndex: 'createByDesc', sorter: true, key: 'create_by', width: 100, },
        { title: this.$t("create_time"), dataIndex: 'createTime', sorter: true, key: 'create_time', width: 155, },
        { title: this.$t("update_by"), dataIndex: 'updateByDesc', sorter: true, key: 'update_by', width: 100, },
        { title: this.$t("update_time"), dataIndex: 'updateTime', sorter: true, key: 'update_time', width: 155, },
        { title: this.$t("action"), key: 'action', scopedSlots: { customRender: 'action' }, width: 60, fixed: 'right', }
      ]
      return columns
    }
  },
  mounted() {
    this.fetch();
  },
  methods: {
    handleSearch (e) {
      if(e){
        this.pagination.current = 1;
        e.preventDefault();
      }
      this.fetch()
    },
    handleReset () {
      this.form.resetFields();
      this.handleSearch()
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination.pageSize = pagination.pageSize
      this.pagination.current = pagination.current
      this.pagination.sortField = ''
      this.pagination.sortOrder = ''
      if (sorter.column) {
        this.pagination.sortField = sorter.columnKey
        this.pagination.sortOrder = sorter.order
      }
      this.fetch({
        ...filters,
      });
    },
    fetch (param = {}) {
      this.form.validateFields((err, formValue) => {
        if (err) {
          return;
        }
        this.loading = true
        axios({
          method: 'get',
          url: '/infra/param/find',
          params: {
            pageSize: this.pagination.pageSize,
            pageNum: this.pagination.current,
            sortField: this.pagination.sortField,
            sortOrder: this.pagination.sortOrder,
            ...param,
            ...formValue
          },
        }).then((res) => {
          this.pagination.total = res.data.total;
          this.data = res.data.rows;
        }).catch(() => {
          
        }).finally(() => {
          this.loading = false;
        })
      });
    },
  },
}
</script>